package com.hs.components.dwr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hs.HsBase;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;

public class DwrSelect {
   
	/**
	 * 根据父节点id以及类型查询下拉框数据
	 * @param parentId
	 * @param type
	 * @return
	 */
	public Map getSelectData(String parentId,String type){
		Map dataMap = new HashMap();
		try {
			DBOperator dbo = DatabaseConnection.getDBOperator();
			List dataList = dbo.select(HsBase.getAnsiSqlMap().get("sort.select")+"", new Object[]{type,parentId});
			
			for(int i =0;i<dataList.size();i++){
				Map m = (Map)dataList.get(i);
				dataMap.put("id", (String)m.get("ID"));
				dataMap.put("sortName", (String)m.get("SORT_NAME"));
			}
		} catch (Exception e) { 
			e.printStackTrace();
		}
		dataMap.put("test", "test");
		return dataMap;
	}
	/**
	 * 根据父节点id以及类型查询下拉框数据
	 * @param parentId
	 * @param type
	 * @return
	 */
	public String getSelectHTMLStr(String parentId,String type){ 
		Object[] objs = new Object[]{parentId,type};
		System.out.println(objs);
		String sql = HsBase.getAnsiSqlMap().get("sort.select")+"";
		String returnStr ="";
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		//List dataList = getDataList(sql,objs); 
		List dataList = getDataList(sql,parentId,type);
		System.out.println(dataList);
		for(int i =0;i<dataList.size();i++){
			Map m = (Map)dataList.get(i);
			builder.append("{ name:'" + (String)m.get("SORT_NAME") + "', id:'" +(Long)m.get("ID") + "' },"); 
		} 
		if(builder.toString().endsWith(","))
			returnStr = builder.substring(0,builder.length()-1); 
		else
			returnStr = "[";
		returnStr += "]"; 
		return returnStr;
	}
	private List getDataList(String sql,String parentId,String type){ 
		List dataList = new ArrayList();
		try {
			DBOperator dbo = DatabaseConnection.getDBOperator();
			dataList = dbo.select(sql, new Object[]{type,parentId});
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return dataList;
	}
}
